#include <header.h>

int main(){
    char *port = "8080";
    char *ip = "192.168.233.128";
    int socket_fd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(atoi(port));
    sockaddr.sin_addr.s_addr = inet_addr(ip);

    bind(socket_fd,(struct sockaddr*)&sockaddr,sizeof(sockaddr));
    listen(socket_fd,10);

    int nat_fd = accept(socket_fd,NULL,NULL);

    fd_set set;

    while(1){
        FD_ZERO(&set);
        FD_SET(STDIN_FILENO,&set);
        FD_SET(nat_fd,&set);
        
        struct timeval tv;
        tv.tv_sec = 10;
        tv.tv_usec = 0;

        int num = select(nat_fd + 1,&set,NULL,NULL,&tv);

        if(num == 0 ){
            printf("超时\n");
        }


        char buf[100] = {0};

        if(FD_ISSET(nat_fd,&set)){
            ssize_t ret = recv(nat_fd,buf,sizeof(buf),0);
            if(ret == 0){
                printf("对方断开链接 \n");
                break;
            }
            printf("buf1: %s \n",buf);

        }

        if(FD_ISSET(STDIN_FILENO,&set)){
            bzero(buf,sizeof(buf));
            ssize_t ret = read(STDIN_FILENO,buf,sizeof(buf));
            send(nat_fd,buf,ret,0);

        }
    }
    close(socket_fd);
    close(nat_fd);
    return 0;
}

